package cv.vender;

import android.app.Activity;
import android.content.Context;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

//公共参数类
@SuppressWarnings("JavaDoc")
public class clsArgs {
    private static final String CVND = "CVND2";
    private static final int PRODUCT_COUNT = 8;
    private static String path = "config.ini";
    private static clsArgs inst;
    private int Xuhao = 1;
    private String aesKey;
    private String commid;
    private int baudrate;
    private String printer_commid;
    private int printer_baudrate;
    private int bills;
    private int coins;
    private String WinTitle;
    private String imgae_log_file;
    private String[] product = new String[PRODUCT_COUNT]; //暂时是8个产品
    private int venderid;
    /**
     * 服务器url地址
     */
    private String serverurl;
    /**
     * 和服务器通信的密钥
     */
    private String pwd;
    private int no_operation_time;
    /**
     * 用于给系统检查是否文件之前就存在
     */
    private String flg;
    private int image_interval;
    private boolean UseWx;
    private boolean UseAl;
    private String mTitle;
    private String mWelcomTips;

    /**
     * 得到netconfig.properties配置文件中的所有配置属性
     *
     * @param startMainActivity
     * @return Properties对象
     */
    private static Properties getNetConfigProperties(Activity startMainActivity) throws IOException {
        Properties props = new Properties();
        InputStream in = startMainActivity.openFileInput(path);

        props.load(in);
        in.close();
        return props;
    }

    //保存配置文件
    private static boolean setNetConfigProperties(Properties properties, Activity startMainActivity) throws IOException {
        //File extDir = Environment.getExternalStorageDirectory();
        FileOutputStream s = startMainActivity.openFileOutput(path, Context.MODE_WORLD_WRITEABLE);//文件名为config.ini
        properties.store(s, "");
        s.close();
        s.flush();
        return true;
    }


    //加载参数
    public static void loadargs(Activity startMainActivity) throws IOException {
        if (inst == null) {
            inst = new clsArgs();
            if (!inst.ReadPropertiesFile(startMainActivity)) {
                inst.CreatePropertiesFile(startMainActivity);
            }
            inst.ReadPropertiesFile(startMainActivity);
        }
    }

    public static clsArgs getInst() {
        return inst;
    }

    public static void setInst(clsArgs inst) {
        clsArgs.inst = inst;
    }

    private void CreatePropertiesFile(Activity startMainActivity) throws IOException {
        Properties props;
        if (!inst.flg.equals(CVND)) {
            props = new Properties();
            this.flg = CVND;
            props.setProperty("Bills", "65535");
            props.setProperty("Coins", "65535");
            props.setProperty("Baudrate", "9600");
            props.setProperty("printer_baudrate", "38400");
            props.setProperty("Venderid", "999");
            props.setProperty("Commid", "/dev/ttyS2");
            props.setProperty("printer_commid", "/dev/ttyS3");//串行终端设备
            props.setProperty("No_operation_time", "99");
            props.setProperty("pwd", "88888888");
            props.setProperty("aesKey", "1234567890123456");
            props.setProperty("serverurl", "http://ww.vendertec.top/Ticket/SetPara3");
            props.setProperty("image_interval", "2000");
            props.setProperty("Xuhao", "1");
            props.setProperty("WinTitle", "欢迎使用自动售票机");
            props.setProperty("imgae_log_file", "log.jpg");
            props.setProperty("mTitle", "科威尼迪科技");
            props.setProperty("mWelcomTips", "欢迎使用科威尼迪快剪");
            for (int i = 0; i < PRODUCT_COUNT; i++) {//循环设置配置文件
                props.setProperty("product" + i, "");
            }

            props.setProperty("flg", CVND);
            props.setProperty("price", "1600");
            ////保存配置文件
            setNetConfigProperties(props, startMainActivity);
        }
    }


    /**
     * 读取配置文件方法
     *
     * @param startMainActivity
     * @return
     * @throws IOException
     */
    private boolean ReadPropertiesFile(Activity startMainActivity) throws IOException {
        this.flg = "";
        Properties props;//声明配置文件
        try {
            props = getNetConfigProperties(startMainActivity);//获取配置文件

            this.flg = (props.getProperty("flg", ""));

            if (!this.flg.equals(CVND)) {//证明没有配置文件  ->然后就不会添加产品->就会报提示
//                Toast.makeText(startMainActivity, "证明没有配置文件", Toast.LENGTH_LONG).show();
                return false;
            }

            this.UseAl = clsToolBox.ParseInt(props.getProperty("UseAl", "0")) == 1;
            this.UseWx = clsToolBox.ParseInt(props.getProperty("UseWx", "0")) == 1;
            this.bills = (clsToolBox.ParseInt(props.getProperty("bills", "65535")));
            this.coins = (clsToolBox.ParseInt(props.getProperty("coins", "65535")));
            this.baudrate = (clsToolBox.ParseInt(props.getProperty("Baudrate", "9600")));
            this.printer_baudrate = (clsToolBox.ParseInt(props.getProperty("printer_baudrate", "38400")));
            this.venderid = (clsToolBox.ParseInt(props.getProperty("Venderid", "999")));
            this.commid = (props.getProperty("Commid", "/dev/ttyS3"));
            this.printer_commid = (props.getProperty("printer_commid", "/dev/ttyS3"));
            this.no_operation_time = (clsToolBox.ParseInt(props.getProperty("No_operation_time", "99")));
            this.pwd = (props.getProperty("pwd", "88888888"));
            this.setAesKey((props.getProperty("aesKey", "1234567890123456")));
            this.serverurl = (props.getProperty("serverurl", "http://ww.vendertec.top/Ticket/SetPara3"));
            this.image_interval = (clsToolBox.ParseInt(props.getProperty("image_interval", "5000")));
            this.Xuhao = (clsToolBox.ParseInt(props.getProperty("Xuhao", "1")));
            this.WinTitle = props.getProperty("WinTitle", "欢迎使用自动售票机");
            this.imgae_log_file = props.getProperty("imgae_log_file", "log.jpg");
            this.mTitle = props.getProperty("mTitle", "科威尼迪科技");
            this.mWelcomTips = props.getProperty("mWelcomTips", "欢迎使用科威尼迪快剪");
            for (int i = 0; i < PRODUCT_COUNT; i++) {//这里是循环添加产品哦
                this.product[i] = props.getProperty("product" + i, "");
            }
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    public String[] getProduct() {
        return product;
    }

    //保存配置文件--》点击按钮时候的
    public void SavePara() {
        Properties props;
        try {
            props = getNetConfigProperties(clsPublicPara.activity);

            for (int i = 0; i < PRODUCT_COUNT; i++) {

                props.setProperty("product" + i, product[i]);
            }
            props.setProperty("Venderid", String.valueOf(venderid));
            props.setProperty("serverurl", serverurl);
            props.setProperty("coins", String.valueOf(coins));
            props.setProperty("bills", String.valueOf(bills));
            props.setProperty("UseAl", UseAl ? "1" : "0");
            props.setProperty("UseWx", UseWx ? "1" : "0");
            props.setProperty("mTitle", mTitle);
            props.setProperty("mWelcomTips", mWelcomTips);
            props.setProperty("Commid", commid);

            //保存配置文件
            setNetConfigProperties(props, clsPublicPara.activity);
        } catch (IOException e1) {

            e1.printStackTrace();
        }
    }

    public String getCommid() {
        return commid;
    }

    public void setCommid(String commid) {
        this.commid = commid;
    }

    public int getBaudrate() {
        return baudrate;
    }

    public void setBaudrate(int baudrate) {
        this.baudrate = baudrate;
    }

    public int getBills() {
        return bills;
    }

    public void setBills(int bills) {
        this.bills = bills;
    }

    public int getCoins() {
        return coins;
    }

    public void setCoins(int coins) {
        this.coins = coins;
    }

    public int getVenderid() {
        return venderid;
    }

    public void setVenderid(int venderid) {
        this.venderid = venderid;
    }

    public String getServerurl() {
        return serverurl;
    }

    public void setServerurl(String serverurl) {
        this.serverurl = serverurl;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getNo_operation_time() {
        return no_operation_time;
    }

    public void setNo_operation_time(int no_operation_time) {
        this.no_operation_time = no_operation_time;
    }

    public String getFlg() {
        return flg;
    }

    public void setFlg(String flg) {
        this.flg = flg;
    }

    public int getImage_interval() {
        return image_interval;
    }

    public void setImage_interval(int image_interval) {
        this.image_interval = image_interval;
    }

    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public String getPrinter_commid() {
        return printer_commid;
    }

    public void setPrinter_commid(String printer_commid) {
        this.printer_commid = printer_commid;
    }

    public int getPrinter_baudrate() {
        return printer_baudrate;
    }

    public void setPrinter_baudrate(int printer_baudrate) {
        this.printer_baudrate = printer_baudrate;
    }

    public String getWinTitle() {
        return WinTitle;
    }

    public void setWinTitle(String winTitle) {
        WinTitle = winTitle;
    }

    public String getImgae_log_file() {
        return imgae_log_file;
    }

    public void setImgae_log_file(String imgae_log_file) {
        this.imgae_log_file = imgae_log_file;
    }


    public boolean isUseWx() {
        return UseWx;
    }

    public void setUseWx(boolean useWx) {
        UseWx = useWx;
    }

    public boolean isUseAl() {
        return UseAl;
    }

    public void setUseAl(boolean useAl) {
        UseAl = useAl;
    }

    public String getmWelcomTips() {
        return mWelcomTips;
    }

    public void setmWelcomTips(String mWelcomTips) {
        this.mWelcomTips = mWelcomTips;
    }
}
